//
//  ChatToolBar.m
//  AILove
//
//  Created by Ace Portgas on 2024/1/7.
//

#import "ChatToolBar.h"
@interface ChatToolBar()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField * textField;
@property (nonatomic,strong) UIButton * sendBtn;
@end
@implementation ChatToolBar

-(void)SQ_UIViewForSelfView{
    self.backgroundColor = [UIColor clearColor];
    // gradient
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,SCREEN_WIDETH,86 + ZXSafeAreaBottom);
    gl.startPoint = CGPointMake(0.5, 0);
    gl.endPoint = CGPointMake(0.5, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.0].CGColor, (__bridge id)[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8000].CGColor];
    gl.locations = @[@(0), @(0.2f)];

    // blur
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    visualView.frame = CGRectMake(0,692,375,120);

    [self.layer addSublayer:gl];
    
    _textField = [UITextField new];
    _textField.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF" alpha:0.24];
    _textField.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
    _textField.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    _textField.delegate = self;
    _textField.layer.cornerRadius = 22;
    [self addSubview:_textField];
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 16, 1)];
    UIView * rightView = leftView;
    _textField.leftView = leftView;
    _textField.rightView = rightView;
    _textField.leftViewMode = UITextFieldViewModeAlways;
    _textField.rightViewMode = UITextFieldViewModeAlways;
    
    _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sendBtn.backgroundColor = [UIColor clearColor];
    [_sendBtn setImage:SQGetImage(@"发送") forState:UIControlStateNormal];
    _sendBtn.imageView.contentMode = UIViewContentModeCenter;
    [_sendBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_sendBtn];
    [_sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(44, 44));
        make.top.mas_equalTo(self).offset(32);
        make.right.mas_equalTo(self).offset(-15);
    }];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(32);
        make.left.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self.sendBtn.mas_left).offset(-10);
        make.height.mas_offset(44);
    }];
}

-(void)sendMessage{
    if (self.textField.text.length > 0) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(sendMessageWithText:)]) {
            [self.delegate sendMessageWithText:self.textField.text];
//            self.textField.text = @"";
        }
    }else{
        return;
    }
}

-(void)clearText{
    self.textField.text = @"";
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length == 0) {
        return NO;
    }else{
        [self sendMessage];
        return YES;
    }
    return YES;
}

@end
